/**
 * 日志告警历史控制器
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Controller, 
  Get, 
  Query, 
  UseGuards,
  HttpStatus,
} from '@nestjs/common';
import { 
  ApiTags, 
  ApiOperation, 
  ApiResponse,
  ApiQuery,
} from '@nestjs/swagger';
import { LogAlertHistoryService } from './log-alert-history.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermissions } from '../auth/decorators/permissions.decorator';
import { LogAlertHistory } from './entities/log-alert-history.entity';

@ApiTags('日志告警历史')
@UseGuards(JwtAuthGuard)
@Controller('log-alert-history')
export class LogAlertHistoryController {
  constructor(private readonly logAlertHistoryService: LogAlertHistoryService) {}

  @Get()
  @RequirePermissions('log:alert:history')
  @ApiOperation({ summary: '查询告警历史' })
  @ApiQuery({ name: 'startTime', required: false, type: Date })
  @ApiQuery({ name: 'endTime', required: false, type: Date })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '查询成功',
    type: [LogAlertHistory] 
  })
  async findByTimeRange(
    @Query('startTime') startTime?: Date,
    @Query('endTime') endTime?: Date,
  ) {
    const histories = await this.logAlertHistoryService.findByTimeRange(
      startTime || new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // 默认查询最近7天
      endTime || new Date(),
    );
    return histories;
  }

  @Get('recent')
  @RequirePermissions('log:alert:history')
  @ApiOperation({ summary: '获取最近告警历史' })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '查询成功',
    type: [LogAlertHistory] 
  })
  async findRecent(@Query('limit') limit?: number) {
    return await this.logAlertHistoryService.findRecent(limit);
  }

  @Get('stats')
  @RequirePermissions('log:alert:history')
  @ApiOperation({ summary: '获取告警统计信息' })
  @ApiQuery({ name: 'startTime', required: false, type: Date })
  @ApiQuery({ name: 'endTime', required: false, type: Date })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '查询成功' 
  })
  async getStatistics(
    @Query('startTime') startTime?: Date,
    @Query('endTime') endTime?: Date,
  ) {
    return await this.logAlertHistoryService.getStatistics(
      startTime || new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), // 默认统计最近30天
      endTime || new Date(),
    );
  }
} 